/*
 * @Description  : 
 * @Author       : zhao
 * @Date         : 2022-07-11 09:16:54
 * @LastEditors  : zhao
 * @LastEditTime : 2023-02-01 17:18:21
 */
import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { UserInfosStates } from './interface';
import { Session } from '/@/utils/storage';
import { useLogin } from '/@/api/login/index.ts';
import { GetUserInfo, GetSysUserInfo, Registered, UpdateSysUser, GetWorkCompany, PasswordRecord, DeleteSysUser, CSGetBorrowingForm, GetArchiveList } from '/@/api/userInfo.js';
import { ElMessage } from 'element-plus';
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
type Result = {
	username: string
	password: string
}
const Login = (data: Result): Promise<any> => {
	return new Promise((resolve) => {
		useLogin(data).then((res: any) => {
			if (res.code === 200) {

				resolve(res)
			} else {
				resolve(res)
				ElMessage.error(res.msg || "请求失败")
			}

		})
	})
}

export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosStates => ({
		userInfos: {
			userName: '',
			photo: '',
			time: 0,
			roles: [],
			authBtnList: [],
		},
	}),
	actions: {
		async setUserInfos() {

			// 模拟数据，请求接口时，记得删除多余代码及对应依赖的引入
			const userName = Cookies.get('userName');
			// 模拟数据
			let defaultRoles: Array<string> = [];
			let defaultAuthBtnList: Array<string> = [];
			// admin 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
			let adminRoles: Array<string> = ['admin'];
			// admin 按钮权限标识
			let adminAuthBtnList: Array<string> = ['btn.add', 'btn.del', 'btn.edit', 'btn.link'];
			// test 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
			let testRoles: Array<string> = ['common'];
			// test 按钮权限标识
			let testAuthBtnList: Array<string> = ['btn.add', 'btn.link'];
			// 不同用户模拟不同的用户权限
			if (userName === 'admin') {
				defaultRoles = adminRoles;
				defaultAuthBtnList = adminAuthBtnList;
			} else {
				defaultRoles = testRoles;
				defaultAuthBtnList = testAuthBtnList;
			}
			// 用户信息模拟数据
			const userInfos = {
				userName: userName,
				photo:
					userName === 'admin'
						? 'https://pics5.baidu.com/feed/314e251f95cad1c8251e3f528d9a280ec83d514e.jpeg'
						: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=317673774,2961727727&fm=26&gp=0.jpg',
				time: new Date().getTime(),
				roles: defaultRoles,
				authBtnList: defaultAuthBtnList,
			};
			// 存储用户信息到浏览器缓存
			Session.set('userInfo', userInfos);

			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				this.userInfos = userInfos;
			}
		},

		async login(data: any) {
			const result = await Login(data)
			return result

		},
		//获取借阅用户
		async getUserInfo(data: any): Promise<any> {

			return new Promise((resolve, reject) => {
				GetUserInfo(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})

		},
		//CS新增用户
		async registered(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				Registered(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						ElMessage.success(res.msg || "新增成功")
					} else {
						ElMessage.error(res.msg || "新增失败")
						reject(res)
					}

				})

			})

		},
		//CS修改用户
		async updateSysUser(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				UpdateSysUser(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
						ElMessage.success(res.msg || "修改成功")
					} else {
						ElMessage.error(res.msg || "修改失败")
						reject(res)
					}

				})

			})

		},
		//获取系统用户
		async getSysUserInfo(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				GetSysUserInfo(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}

				})

			})

		},
		//CS获取借阅单位
		getWorkCompany(): Promise<any> {
			return new Promise((resolve, reject) => {
				GetWorkCompany().then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}

				})

			})

		},
		//密码重置
		passwordRecord(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				PasswordRecord(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})
		},
		//CS删除管理员用户
		DeleteSysUser(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				DeleteSysUser(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})
		},
		//实时通信
		CSGetBorrowingForm(data: any): Promise<any> {
			return new Promise((resolve, reject) => {
				CSGetBorrowingForm(data).then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})
		},
		//矿种
		GetArchiveList(): Promise<any> {
			return new Promise((resolve, reject) => {
				GetArchiveList().then((res: any) => {
					if (res.code === 200) {
						resolve(res)
					} else {
						reject(res)
					}
				})
			})
		},
	},
});
